/*
 * PosManager.h
 *
 *  Created on: 2012-9-8
 *      Author: yangentao@gmail.com
 */

#ifndef POSMANAGER_H_
#define POSMANAGER_H_
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>

#include <boost/shared_ptr.hpp>
#include "../util/util.h"

using namespace std;

class Pos;
class PosManager;

class Pos {
	friend class PosManager;
private:
	boost::shared_ptr<int> value;
	PosManager* mgr;
public:
	Pos();
	Pos(PosManager* mgr_, boost::shared_ptr<int> value_);
	~Pos();

	void release();

	inline int index() {
		if(!isValid()){
			throw InvalidIndexException();
		}
		return *(value.get());
	}

	inline bool isValid() {
		return mgr && value.get() && (*(value.get()) >=0);
	}
};

class PosManager {
	friend class Pos;
	typedef map<int, boost::shared_ptr<int> > PosMap;
private:
	PosMap posMap;
	void releasePos(int index);
public:
	Pos createPos(int index);
//	void updateRange(int from, int delta);
//	void removeRange(int from, int length);
	inline int size(){
		return posMap.size();
	}
	void onTextReplaced(int from, int removLength, int insertLength);
};

#endif /* POSMANAGER_H_ */
